package org.example.mapper;

import org.apache.ibatis.annotations.*;
import org.example.common.entity.Product;

import java.util.List;

@Mapper
public interface ProductMapper {

    // 插入一条商品记录
    @Insert("INSERT INTO product (name, category_id, brand_id, price, final_price, stock, status, is_hot, is_recommended, " +
            "cover_image, created_by, updated_by, created_at, updated_at, is_deleted) " +
            "VALUES (#{name}, #{categoryId}, #{brandId}, #{price}, #{finalPrice}, #{stock}, #{status}, #{isHot}, " +
            "#{isRecommended}, #{coverImage}, #{createdBy}, #{updatedBy}, NOW(), NOW(), 0)")
    int insert(@Param("product") Product product);

    // 根据 ID 更新商品记录（选择性更新，只更新非空字段）
    @Update("<script>" +
            "UPDATE product" +
            "<set>" +
            "<if test='product.name != null'>name=#{product.name},</if>" +
            "<if test='product.categoryId != null'>category_id=#{product.categoryId},</if>" +
            "<if test='product.brandId != null'>brand_id=#{product.brandId},</if>" +
            "<if test='product.price != null'>price=#{product.price},</if>" +
            "<if test='product.finalPrice != null'>final_price=#{product.finalPrice},</if>" +
            "<if test='product.stock != null'>stock=#{product.stock},</if>" +
            "<if test='product.status != null'>status=#{product.status},</if>" +
            "<if test='product.isHot != null'>is_hot=#{product.isHot},</if>" +
            "<if test='product.isRecommended != null'>is_recommended=#{product.isRecommended},</if>" +
            "<if test='product.coverImage != null'>cover_image=#{product.coverImage},</if>" +
            "<if test='product.updatedBy != null'>updated_by=#{product.updatedBy},</if>" +
            "updated_at=NOW()" +
            "</set>" +
            "WHERE id=#{product.id}" +
            "</script>")
    int updateByPrimaryKeySelective(@Param("product") Product product);

    // 根据主键更新所有字段
    @Update("UPDATE product SET name=#{product.name}, category_id=#{product.categoryId}, brand_id=#{product.brandId}, " +
            "price=#{product.price}, final_price=#{product.finalPrice}, stock=#{product.stock}, status=#{product.status}, " +
            "is_hot=#{product.isHot}, is_recommended=#{product.isRecommended}, cover_image=#{product.coverImage}, " +
            "updated_by=#{product.updatedBy}, updated_at=NOW() WHERE id=#{product.id}")
    int update(@Param("product") Product product);

    // 根据主键删除（物理删除）
    @Delete("DELETE FROM product WHERE id=#{id}")
    int deleteByPrimaryKey(@Param("id") Long id);

    // 根据主键查询
    @Select("SELECT * FROM product WHERE id=#{id}")
    Product selectByPrimaryKey(@Param("id") Long id);

    // 根据 ID 删除商品记录（逻辑删除）
    @Update("UPDATE product SET is_deleted=1, updated_at=NOW() WHERE id=#{id}")
    int deleteById(@Param("id") Long id);

    // 根据 ID 查询商品记录
    @Select("SELECT * FROM product WHERE id=#{id} AND is_deleted=0")
    Product selectById(@Param("id") Long id);

    // 查询所有未删除的商品记录
    @Select("SELECT * FROM product WHERE is_deleted = 0")
    List<Product> selectAll();

    // 根据分类 ID 查询商品记录
    @Select("SELECT * FROM product WHERE category_id = #{categoryId} AND is_deleted = 0")
    List<Product> selectByCategoryId(@Param("categoryId") Long categoryId);

    // 查询热销商品
    @Select("SELECT * FROM product WHERE is_hot=1 AND is_deleted=0 AND status=1 ORDER BY updated_at DESC")
    List<Product> selectHotProducts();

    // 查询推荐商品
    @Select("SELECT * FROM product WHERE is_recommended=1 AND is_deleted=0 AND status=1 ORDER BY updated_at DESC")
    List<Product> selectRecommendedProducts();

    // 根据是否热销查询商品
    @Select("SELECT * FROM product WHERE is_hot=#{isHot} AND is_deleted=0")
    List<Product> selectByIsHot(@Param("isHot") Integer isHot);

    // 根据是否推荐查询商品
    @Select("SELECT * FROM product WHERE is_recommended=#{isRecommended} AND is_deleted=0")
    List<Product> selectByIsRecommended(@Param("isRecommended") Integer isRecommended);

    // 更新热销状态
    @Update("UPDATE product SET is_hot=#{status}, updated_at=NOW() WHERE id=#{id}")
    int updateHotStatus(@Param("id") Long id, @Param("status") Integer status);

    // 更新推荐状态
    @Update("UPDATE product SET is_recommended=#{status}, updated_at=NOW() WHERE id=#{id}")
    int updateRecommendedStatus(@Param("id") Long id, @Param("status") Integer status);

    // 查询新商品列表（按创建时间倒序） （最近30天）
    @Select("SELECT * FROM product WHERE is_deleted=0 AND status=1 " +
            "AND created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY) " +
            "ORDER BY created_at DESC")
    List<Product> selectNewProducts();
}